package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.bean.BusInfoLineQuery;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;

import java.util.List;

/**
 * @Author WLL
 * @Time: 2019/4/27 16:13
 */
public class LineQueryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "LineQueryAdapter";
    private Context mContext;
    private List<BusInfoLineQuery> mBusInfoLineQueryList;

    public LineQueryAdapter(Context context, List<BusInfoLineQuery> mBusInfoLineQueryList) {
        this.mContext = context;
        this.mBusInfoLineQueryList = mBusInfoLineQueryList;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mBusInfoLineQueryList != null ? mBusInfoLineQueryList.size() : 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_line_query, parent, false);
        return new LineQueryHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        LineQueryHolder lineQueryHolder = (LineQueryHolder) holder;
        lineQueryHolder.mBusName.setText(mBusInfoLineQueryList.get(position).getName());
        lineQueryHolder.mBusLine.setText(mBusInfoLineQueryList.get(position).getLine());
//        if (getItemCount() - 1 > 0 && position == getItemCount() - 1)
//            lineQueryHolder.mBottomLine.setVisibility(View.GONE);
//        else
//            lineQueryHolder.mBottomLine.setVisibility(View.VISIBLE);
    }

    public class LineQueryHolder extends RecyclerView.ViewHolder {
        TextView mBusName;
        TextView mBusLine;
        View mBottomLine;

        public LineQueryHolder(View view) {
            super(view);
            mBusName = view.findViewById(R.id.item_line_query_bus_name_text);
            mBusLine = view.findViewById(R.id.item_line_query_bus_line_text);
            mBottomLine = view.findViewById(R.id.item_line_query_bottom_line);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

}
